<template>
  <div class="app-container">
    <upload-excel-component :on-success="handleSuccess" :before-upload="beforeUpload" />
  </div>
</template>

<script>
import { formatDate } from '@/utils'
import { importEmployee } from '@/api/employees'
import UploadExcelComponent from '@/components/UploadExcel/index.vue'
export default {
  name: 'UploadExcel',
  components: { UploadExcelComponent },
  data() {
    return {
      tableData: [],
      tableHeader: []
    }
  },
  methods: {
    beforeUpload(file) {
      const isLt1M = file.size / 1024 / 1024 < 1
      if (isLt1M) {
        return true
      }
      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false
    },
    async handleSuccess({ results, header }) {
      // console.log(header, 7777) //  ["手机号", "姓名", "入职日期", "转正日期", "工号"] 7777
      // console.log(results, 8888) //  [{},{},{}]
      var obj = {
        '入职日期': 'correctionTime',
        '手机号': 'mobile',
        '转正日期': 'timeOfEntry',
        '姓名': 'username',
        '工号': 'workNumber'
      }

      var newResult = results.map(item => {
        //  item==
        /* // {
					'入职日期': '44266',
        // '手机号': '13142239879',
        // '转正日期': '43719',
        // '姓名': '高大山1',
        // '工号': '10189'} */
        var newObj = {}
        var keyArr = Object.keys(obj) //  [入职日期,手机号,转正日期,姓名,工号]
        keyArr.forEach(item2 => { //  item2=>入职日期,手机号,转正日期,姓名,工号
				  // newObj[obj[item2]] = item[item2]
          if (obj[item2] === 'correctionTime' || obj[item2] === 'timeOfEntry') {
            newObj[obj[item2]] = formatDate(item[item2], '-')
            // console.log(newObj, 889999)
          } else {
            newObj[obj[item2]] = item[item2]
          }

          //  obj[item2]===correctionTime ,mobile....    {correctionTime}
          // item[item2]  //  value值   44266
          // {correctionTime:44266,mobile:13142239879}
        })
        return newObj
      })
      console.log(newResult, 777666)

      // obj['入职日期']===='correctionTime'

      await importEmployee(newResult) //  批量添加
			 this.$message({
        message: '恭喜你，这是一条成功消息',
        type: 'success'
      })
      this.$router.back()//  返回员工页面
    }
  }
}
</script>
